package com.wangsb.controller;

import com.wangsb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author: shenbing.wang
 * @CreateDate: 2021/2/19 23:18
 * @Version: 1.0
 * @Description:
 */
@Controller
//@SessionAttributes作用在类上面，用于读取session
// 从model中获取指定的属性
//底层会从model中去找一个叫type的属性
//找到了会将type设置一份到session中
//这种方式是依赖model的
@SessionAttributes("type")
public class DTVController {

    @RequestMapping("/servletApi")
    public String servletApi(HttpServletRequest request) {
        request.setAttribute("type", "servletApi");
        return "main";
    }

    @RequestMapping("/model")
    public String model(Model model) {
        model.addAttribute("type", "model");
        return "main";
    }

    @RequestMapping("/modelMap")
    public String modelMap(ModelMap modelMap) {
        modelMap.addAttribute("type", "modelMap");
        return "main";
    }

    @RequestMapping("/map")
    public String map(Map map) {
        map.put("type", "map");
        return "main";
    }

    @RequestMapping("/modelAndView")
    public ModelAndView modelAndView() {
        ModelAndView mv = new ModelAndView("main");
        mv.addObject("type", "map");
        return mv;
    }


    @RequestMapping("/servletSession")
    public String servletSession(HttpSession session) {
        session.setAttribute("type", "servletSession");
        return "main";
    }

    @Autowired
    private HttpSession session;

    @RequestMapping("/autowiredSession")
    public String autowiredSession() {
        session.setAttribute("type", "autowiredSession");
        return "main";
    }

    @RequestMapping("/annotationSession")
    public String annotationSession(@SessionAttribute(value = "type", required = false) String type) {
        System.out.println("type = " + type);
        return "main";
    }

    @RequestMapping("/modelAttribute")
    public String modelAttribute() {
        session2.setAttribute("type", "modelAttribute-session");
        return "main";
    }

    HttpSession session2;

    //@ModelAttribute
    //public void modelAttribute(Model model) {
    //    System.out.println("modelAttribute");
    //    User user = new User(1, "123456", "wsb");
    //    System.out.println("user = " + user);
    //    model.addAttribute("user", user);
    //
    //}

    @RequestMapping("/update")
    public String update(User user) {
        System.out.println("user = " + user);
        return "main";
    }

}
